package com.xian.cloud.rpc;

import com.codingapi.tx.annotation.ITxTransaction;
import com.codingapi.tx.annotation.TxTransaction;
import com.xian.cloud.entity.UserEntity;
import com.xian.cloud.face.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xlr
 * @Date: Created in 下午6:07 2018/11/11
 */
@RestController
public class UserRpcController implements UserServiceApi,ITxTransaction {

    private static final Logger log = LoggerFactory.getLogger( UserRpcController.class );
    @Autowired
    UserService userService;

    /**
     * 根据名称获取ID
     *
     * @param id
     * @return
     */
    @Override
    public List<UserEntity> fingById(Long id) {
        List<UserEntity> userEntities = new ArrayList <>(  );
        userEntities.add( userService.selectById( id ) );
        return userEntities;
    }

    /**
     * 保存user
     * 被调用方法上实现分布式事务，必须方法上使用注解@TxTransaction 或者实现 ITxTransaction接口
     * 又或者俩种全部使用，方可实现分布式事务
     * @param user
     * @return
     */
    @Override
    @TxTransaction(isStart=true)
    @Transactional
    public Boolean saveUser(@RequestBody UserEntity user) {
        log.info( user.toString() );
        try{
            userService.insertOrUpdate( user );
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        //为了模拟效果全部返回false
        return false;
    }
}
